package com.qd.common.sys.domain.sendmsg;

import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Data
@Accessors(chain = true)
@Slf4j
public class MsgContent {

    public static final short CONTENT_TXT = 1;
    public static final short CONTENT_HTML = 2;

    /**
     * 消息正文
     */
    private String content;

    /**
     * 消息样式
     */
    private String style;

    /**
     * 消息类型
     */
    private String type;

    /**
     * 事件类型
     */
    private String event;

    /**
     * 消息自定义属性
     */
    private MsgContentProperties customProperties;

    /**
     * 消息动态属性
     */
    private MsgContentProperties dynProperties;

    /**
     * 需要格式化
     */
    private boolean canDoFormat;

    public void free() {
        content = null;
        style = null;
        type = null;
        event = null;
        Optional.ofNullable(customProperties).ifPresent(MsgContentProperties::free);
        Optional.ofNullable(dynProperties).ifPresent(MsgContentProperties::free);
    }

    public boolean isEmptyCustomProperties() {
        if (customProperties == null) {
            return true;
        }
        return customProperties.isEmpty();
    }

    public MsgContentProperties getCustomProperties() {
        if (customProperties == null) {
            customProperties = new MsgContentProperties();
        }
        return customProperties;
    }

    public boolean isEmptyDynProperties() {
        if (dynProperties == null) {
            return true;
        }
        return dynProperties.isEmpty();
    }

    public MsgContentProperties getDynProperties() {
        if (dynProperties == null) {
            dynProperties = new MsgContentProperties();
        }
        return dynProperties;
    }

    public boolean isCanDoFormat() {
        return canDoFormat;
    }

    public void setCanDoFormat(boolean canDoFormat) {
        this.canDoFormat = canDoFormat;
    }

    public void read(JSONObject json) {
        content = json.getString("content");
        style = json.getString("style");
        type = json.getString("type");
        event = json.getString("event");
        buildReadCustomProperties(json);
        buildReadDynProperties(json);

        canDoFormat = json.getBooleanValue("canDoFormat");
    }

    public void write(JSONObject json) {
        json.put("content", content);
        json.put("style", style);
        json.put("type", type);
        json.put("event", event);

        if (!isEmptyCustomProperties()) {
            JSONObject jsonObject = new JSONObject();
            json.put("customProperties", jsonObject);
            getCustomProperties().write(jsonObject);
        }

        if (!isEmptyDynProperties()) {
            JSONObject jsonObject = new JSONObject();
            json.put("dynProperties", jsonObject);
            getDynProperties().write(jsonObject);
        }

        json.put("canDoFormat", canDoFormat);
    }


    private void buildReadDynProperties(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("dynProperties");
        if (jsonObject != null) {
            getDynProperties().read(jsonObject);
        }
    }

    private void buildReadCustomProperties(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("customProperties");
        if (jsonObject != null) {
            getCustomProperties().read(jsonObject);
        }
    }

}
